package Parser;

import scanner.*;
import java.util.ArrayList;
import lowlevel.CodeItem;

/**
 * Start symbol for grammar. 
 */
public class Program {
    Scanner sc;
    private ArrayList<Decl> decls;
    public Program(){
        decls = new ArrayList<Decl>();
    }
    
    public void addDecl(Decl d){
        decls.add(d);
    }
    public ArrayList<Decl> getDecls(){
        return decls;
    }
    void print(){
        CMinusParser.oFile.println("Program:");
        if(decls == null || decls.size() == 0){
            CMinusParser.oFile.println("  null");
        }
        else{
            for(int i = 0; i < decls.size(); i++){
                decls.get(i).print("  ");
            }
        }
        CMinusParser.oFile.flush();
    }
    
    public CodeItem genLLCode(){
        CodeItem firstItem = null;
        CodeItem lastItem = null;
        for(Decl d : decls){
            CodeItem nextItem = d.genLLCode();
            if(lastItem != null){
                lastItem.setNextItem(nextItem);
                if(lastItem == firstItem){
                    firstItem.setNextItem(nextItem);
                }
            } else {
                firstItem = nextItem;
            }
            lastItem = nextItem;
        }
        return firstItem;
    }
    
}
